
*******************************************************
* THIS FILE PREPARES THE OUTCOME VARIABLE USED: 
* ELECTION RESULTS IN SWEDEN per PRECINCT LEVEL   
* ALSO ASSIGN EACH INDIVIDUAL TO A UNIQUE PRECINCT OF RESIDENTS
* WHERE THEY VOTE  *
*******************************************************

clear all
set more off

capture log close
log using "C:\Userdata\Shared\Logs\IntegrationSD\log_data", replace


/////////////
///YEAR 2010
/////////////

*import election results 
import excel "D:\Data\ExtData\ValResData\Riksdagsvalet_2010\VDValRes_2010", firstrow clear

///District-variable with leading zero to match with other election-data
gen str4 z = string(VALDIST,"%04.0f")
gen str2 ll = string(LAN,"%02.0f")
gen str2 kk = string(KOM,"%02.0f")


egen llkkdddd = concat(ll kk z)
destring llkkddd, replace

///Create an *other* category
gen all = Mtal+Ctal+FPtal+KDtal+FItal+MPtal+PPtal+Stal+SDtal+Vtal
gen other = Rostande-all

///Remove district called "onsdagsdistrikt" - foreign votes and postal votes
encode VALDISTRIKT, gen(vald_nmr)

/*3485 are onsdagsdistrikt*/
drop if vald_nmr==3485

*duplicates report llkkdddd /*OK*/

drop LAN KOM z kk ll all SPItal SPIproc OVRtal OVRproc OGtal OGproc

///5668 districts left
save "E:\ProjData\IntegrationSD\temp\2010_electionresults.dta", replace


//////////////
///YEAR 2014
//////////////


clear all

///First: want to add number of legitimate votes
import excel "D:\Data\ExtData\ValResData\Riksdagsval_2014\allmanna-val-2014-huvudresultat-distrikt.xlsx", sheet("Riksdag") firstrow clear

keep Valdistriktskod varavblankavalsedlar Antalröstande Antalogiltigavalsedlar Antalgiltigavalsedlar
rename Valdistriktskod llkkdddd
rename varavblankavalsedlar BLtal
gen BLproc = (BLtal/Antalröstande)*100

duplicates tag llkkdddd, gen(tag)

save "E:\ProjData\IntegrationSD\temp\2014_electionresults_tempfile.dta", replace


///NOW THE MAIN FILE: 
use "D:\Data\ExtData\ValResData\Riksdagsval_2014\R_valdistrikt_2014"

///drop onsdagsdistrikt

encode dist_type, gen(dist)
drop if dist==1

drop dist_type muni_code dist dist_name dist_borders party_votes_prev party_personalvotes

gen str8 llkkdddd = string(dist_code,"%08.0f")
drop dist_code

///wide format

reshape wide party_votes, i(llkkdddd) j(party_abb) string

keep party_votesC party_votesFI party_votesFP party_votesKD ///
party_votesM party_votesMP party_votesPP party_votesS ///
party_votesSD party_votesV dist_votes llkkdddd   

///*other* votes
foreach y in party_votesC party_votesFI party_votesFP party_votesKD ///
party_votesM party_votesMP party_votesPP party_votesS party_votesSD party_votesV {
replace `y'=0 if `y'==.
}
gen all = party_votesC+party_votesFI+party_votesFP+party_votesKD+party_votesM+party_votesMP+party_votesPP+party_votesS+party_votesSD+party_votesV
gen other = dist_votes-all
drop all

///generate variables so they are similar to 2010 data
rename party_votesM Mtal 
gen Mproc = Mtal/dist_votes
rename party_votesC Ctal 
gen Cproc = Ctal/dist_votes 
rename party_votesFP FPtal 
gen FPproc= FPtal/dist_votes 
rename party_votesKD KDtal 
gen KDproc= KDtal/dist_votes 
rename party_votesS Stal 
gen Sproc = Stal/dist_votes 
rename party_votesV Vtal 
gen Vproc = Vtal/dist_votes 
rename party_votesMP MPtal 
gen MPproc = MPtal/dist_votes 
rename party_votesSD SDtal 
gen SDproc= SDtal/dist_votes 
rename party_votesFI FItal 
gen FIproc = FItal/dist_votes 
rename party_votesPP PPtal 
gen PPproc = PPtal/dist_votes 
rename dist_votes Rostande

///5837 districts

merge 1:m llkkdddd using "E:\ProjData\IntegrationSD\temp\2014_electionresults_tempfile.dta"
keep if _merge==3
drop _merge tag

save "E:\ProjData\IntegrationSD\temp\2014_electionresults.dta", replace

erase "E:\ProjData\IntegrationSD\temp\2014_electionresults_tempfile.dta"


/////////////////////////
////DATA FOR PRECINCT 
/////////////////////////

clear all

foreach y in 02 06 10 14 {

///30 million ind. assigned to unique district 2002-2014
use "D:\SCB_ConPol\Stata\VD\Valdist_AV"

///year
keep if Ar==20`y'

///some duplicates - because of recykled personal number. ///
///2006: just 3 ind/6 obs, 
///2010: just 14 ind/28 obs
///2014: just 13 ind/26 obs, so just throw these out for simplicity

duplicates tag LopNr, gen(tag)
*tab tag
drop if tag==1 

*duplicates report LopNr /*OK*/

keep LopNr llkkdddd

///check amount of districts
*bysort llkkdddd: gen district = _n
*count if district==1
 
*2006: 7,222,779 ind assigned to 5783 unique districts 
*2010: 7,516,249 ind assigned to 5669 unique districts
*2014: 7,784,891 ind assigned to 5837 unique districts

save "E:\ProjData\IntegrationSD\temp\20`y'_electiondistrictsperperson.dta", replace

}

